package com.controller.system.agent;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.system.AgentNew;
import com.entity.system.UserInfo;
import com.service.system.agent.AgentNewService;
import com.util.Const;

@Controller
@RequestMapping("/agentNew")
public class AgentNewController {

	@Autowired
	AgentNewService agentNewService;

	@RequestMapping(value = "/nationwideAgentForchinaMap", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> nationwideAgentForchinaMap(HttpServletRequest request) {

		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		UserInfo user = (UserInfo) session.getAttribute(Const.SESSION_USER);
		if (user != null) {
			Map<String, String> paraMap = new HashMap<>();
			AgentNew agentNew = agentNewService.selectByPrimaryKey(user.getId());
			int agentLevel = 0;
			if (agentNew != null) {
				agentLevel = agentNew.getAgentLevel().intValue();
			} 
			if(agentLevel == 3) {
				paraMap.put("group", "province_code");
				paraMap.put("provinceCode", agentNew.getProvinceCode());
			} else if (agentLevel ==0) {
				paraMap.put("group", "province_code");
			} else if (agentLevel == 2) {
				paraMap.put("group", "city_code");
				paraMap.put("cityCode", agentNew.getCityCode());
			} else if (agentLevel == 1) {
				paraMap.put("group", "county_code");
				paraMap.put("countyCode", agentNew.getCountyCode());
			}

			paraMap.put("pageSize", request.getParameter("pageSize"));
			paraMap.put("pageNum", request.getParameter("pageNum"));
			paraMap.put("incomeTypes", "11,21");
			if (StringUtils.isNotEmpty(request.getParameter("provinceCode"))) {
				paraMap.put("provinceCode", request.getParameter("provinceCode"));
				paraMap.put("group", "city_code");
			}
			if (StringUtils.isNotEmpty(request.getParameter("cityCode"))) {
				paraMap.put("cityCode", request.getParameter("cityCode"));
			}
//			if (StringUtils.isNotEmpty(request.getParameter("countyCode"))) {
//				paraMap.put("countyCode", request.getParameter("countyCode"));
//			}
			String isSpecial = 	request.getParameter("isSpecial");
			if(StringUtils.isNotEmpty(isSpecial) && isSpecial.equals("1")) {
				paraMap.put("group", "county_code");
			}
			
			return agentNewService.selectIncomeStatistics(paraMap);
			
		}
		return null;
	}

	
	@RequestMapping(value = "/findAgentIncomeListStatistics", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> findAgentIncomeListStatistics(HttpServletRequest request) {

		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		UserInfo user = (UserInfo) session.getAttribute(Const.SESSION_USER);
		String agentName = request.getParameter("agentName");
		String isSearch = request.getParameter("isSearch");
		Map<String ,Object> map1 = new HashMap<>();
		
		/*数据校验*/
		if(!StringUtils.isEmpty(isSearch) && isSearch.equals("search")) {
			if (StringUtils.isEmpty(agentName)) {
				map1.put("code", Const.ERROR_CODE);
		        map1.put("message","查询失败");
				return map1;
			}
		}
		if (user != null) {
			Map<String, String> paraMap = new HashMap<>();
			AgentNew agentNew = agentNewService.selectByPrimaryKey(user.getId());
			int agentLevel = 0;
			if (agentNew != null) {
				agentLevel = agentNew.getAgentLevel().intValue();
				paraMap.put("agentLevels", String.valueOf(agentLevel));
			} else {
				paraMap.put("agentLevels", "1,2,3");
			}
			if(agentLevel == 3) {
				paraMap.put("countyCode", agentNew.getProvinceCode());
			} else if (agentLevel == 2) {
				paraMap.put("cityCode", agentNew.getCityCode());
			} else if (agentLevel == 1) {
				paraMap.put("countyCode", agentNew.getCountyCode());
			}
			
			paraMap.put("agentName", agentName);
			paraMap.put("pageSize", request.getParameter("pageSize"));
			paraMap.put("pageNum", request.getParameter("pageNum"));

			if (StringUtils.isNotEmpty(request.getParameter("provinceCode"))) {
				paraMap.put("provinceCode", request.getParameter("provinceCode"));
			}
			if (StringUtils.isNotEmpty(request.getParameter("cityCode"))) {
				paraMap.put("cityCode", request.getParameter("cityCode"));
			}
//			if (StringUtils.isNotEmpty(request.getParameter("countyCode"))) {
//				paraMap.put("countyCode", request.getParameter("countyCode"));
//			}
//			String isSpecial = 	request.getParameter("isSpecial");
//			if(StringUtils.isNotEmpty(isSpecial) && isSpecial.equals("1")) {
//				paraMap.put("group", "county_code");
//			}
			
			return agentNewService.queryAgentIncomeList(paraMap);
			
		}
		return null;
	}
	
	
	
	
}
